package com.yun.baymax.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yun.baymax.R;
import com.yun.baymax.activity.NoteEditActivity;
import com.yun.baymax.bean.adapter.NoteInfo;
import com.yun.baymax.cache.UserCache;
import com.yun.baymax.utils.ToastUtils;
import com.yun.lib.utils.L;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2017/6/12.
 */

public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.MyHolder> {
    Context context;
    List<NoteInfo> datas = new ArrayList<>();
    private LayoutInflater inflater;
    private String currentUserName;

    public NoteAdapter(Context context, List<NoteInfo> datas) {
        this.context = context;
        this.datas = datas;
        inflater = LayoutInflater.from(context);
        currentUserName = UserCache.getInstance().getCurrentUser() == null ? null : UserCache.getInstance().getCurrentUser().getUserName();
    }

    public void setDatas(List<NoteInfo> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyHolder myHolder;
        View view;
        if (viewType == 1) {
            view = inflater.inflate(R.layout.item_note1, parent, false);
            myHolder = new MyHolder(view);
        } else if (viewType == 2) {
            view = inflater.inflate(R.layout.item_note2, parent, false);
            myHolder = new MyHolder(view);
        } else {
            view = inflater.inflate(R.layout.item_note3, parent, false);
            myHolder = new MyHolder(view);
        }
        return myHolder;
    }

    @Override
    public int getItemViewType(int position) {
        int length = datas.get(position).getContent().length();
        if (length < 20)
            return 1;
        else if (length < 44)
            return 2;
        else return 3;
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        final NoteInfo noteInfo = datas.get(position);
        if (datas == null)
            return;
        holder.content.setText(noteInfo.getContent());
        holder.time.setText(noteInfo.getUpdatedAt());
        holder.cardView.setBackgroundColor(noteInfo.getColor());
        holder.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, NoteEditActivity.class).putExtra("note", noteInfo));
            }
        });
        holder.content.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                showDialog(noteInfo);
                return false;
            }
        });
    }


    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    private void showDialog(final NoteInfo data) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("确认删除？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                datas.remove(data);
                deleteUP(data);
                notifyDataSetChanged();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    private void deleteUP(NoteInfo data) {
        data.delete(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null)
                    L.i("删除成功");
                else ToastUtils.showShortToast(e.getMessage());
            }
        });
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        private TextView content;
        private TextView time;
        private View cardView;

        public MyHolder(View view) {
            super(view);
            content = (TextView) view.findViewById(R.id.content);
            time = (TextView) view.findViewById(R.id.time);
            cardView = view.findViewById(R.id.card_view);
        }
    }
}